package com.neuedu.his.service;

import com.neuedu.his.dao.*;
import com.neuedu.his.entity.*;
import com.neuedu.his.framework.HisConstants;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class DoctorService {

    private RegisterDao registerDao = new RegisterDao();
    private CheckItemDao checkItemDao = new CheckItemDao();
    private InspectItemDao inspectItemDao = new InspectItemDao();
    private CheckApplyDao checkApplyDao = new CheckApplyDao();

    private InspectApplyDao inspectApplyDao = new InspectApplyDao();

    public boolean submit(Register register) {

        Register oldRegister = registerDao.selectById(register.getId());

        //从页面接收到
        oldRegister.setReadme(register.getReadme());
        oldRegister.setPresent(register.getPresent());
        oldRegister.setPresentTreat(register.getPresentTreat());
        oldRegister.setHistory(register.getHistory());
        oldRegister.setAllergy(register.getAllergy());
        oldRegister.setStatus(HisConstants.REGISTER_STATUS_2); //已提交的状态

        return registerDao.update(oldRegister);

    }

    public boolean checkAplly(HttpServletRequest request) {
        String registerId = request.getParameter("registerId");
        String itemId = request.getParameter("itemId"); //   1,2,3,4

        String[] itemIds = itemId.split(",");
        /**
         *  查询对应的检查项目信息
         *
         *   select * from check_item where id = ?
         *
         *   select * from checkitem where id in (   1,2,3,4    )
         *
         *
         */
        StringBuffer sql = new StringBuffer("select * from check_item where id in ( ");
        for (String id : itemIds) {
            sql.append("?,");
        }
        sql.deleteCharAt(sql.length()-1);
        sql.append(") ");
        
        List<CheckItem> list = checkItemDao.selectList(sql.toString(),itemIds);
        //将checkitem -> checkApply
        boolean success  = true;
        for (CheckItem checkItem : list) {


            CheckApply checkApply = new CheckApply();
            checkApply.setRegisterId(Integer.parseInt(registerId));
            checkApply.setItemId(checkItem.getId());
            checkApply.setItemName(checkItem.getName());
            checkApply.setFee(checkItem.getFee());

            checkApply.setStatus(HisConstants.CHECK_APPLY_STATUS_1); //待缴费

            success = success && checkApplyDao.insert(checkApply);

        }

        return success;
    }

    /**
     * 根据病历号查询申请的 检查项目
     * @param request
     * @return
     */
    public List<CheckApply> checkApllyList(HttpServletRequest request) {
        String registerId = request.getParameter("registerId");

        String sql = "select * from check_apply where register_id = ? ";
        return checkApplyDao.selectList(sql, registerId );
    }

    public boolean inspectApply(HttpServletRequest request) {
        String registerId = request.getParameter("registerId");
        String itemId = request.getParameter("itemId"); //   1,2,3,4

        String[] itemIds = itemId.split(",");


        // 查询对应的检查项目信息  select * from inspect_item where id in (   1,2,3,4    )
        StringBuffer sql = new StringBuffer("select * from inspect_item where id in ( ");
        for (String id : itemIds) {
            sql.append("?,");
        }
        sql.deleteCharAt(sql.length()-1);
        sql.append(") ");

        List<InspectItem> list = inspectItemDao.selectList(sql.toString(),itemIds);
        //将checkitem -> checkApply
        boolean success  = true;
        for (InspectItem checkItem : list) {


            InspectApply inspectApply = new InspectApply();
            inspectApply.setRegisterId(Integer.parseInt(registerId));
            inspectApply.setItemId(checkItem.getId());
            inspectApply.setItemName(checkItem.getName());
            inspectApply.setFee(checkItem.getFee());

            inspectApply.setStatus(HisConstants.CHECK_APPLY_STATUS_1); //待缴费

            success = success && inspectApplyDao.insert(inspectApply);

        }

        return success;
    }

    /**
     * 根据病历号 ，查询开立的  【检验】列表
     * @param request
     * @return  item[]=1&item[]=2
     */
    public List<InspectApply> inspectApllyList(HttpServletRequest request) {
        String registerId = request.getParameter("registerId");

        String sql = "select * from inspect_apply where register_id = ? ";
        return inspectApplyDao.selectList(sql, registerId );
    }

    public boolean finish(Register register) {
        Register oldRegister = registerDao.selectById(register.getId());
        oldRegister.setSuit(register.getSuit());
        //诊疗状态，修改
        return registerDao.update(oldRegister);
    }
}
